
const host = 'http://fanglei.eduyo.cn';
//fanglei.eduyo.cn

module.exports = {
    HOST: host,
    API_ROOT: host + '/',
    API_VERSION: '1.0.0',
    post(options) {
        this.request(options);
    },
    get(options) {
        options.method = 'GET';
        this.request(options);
    },
    delete(options) {
        options.method = 'DELETE';
        this.request(options);
    },
    put(options) {
        options.method = 'PUT';
        this.request(options);
    },
    request(options) {
        var apiRoot = this.API_ROOT;
        var token   = '';
        try {
            token = uni.getStorageSync('JSESSIONID')
        } catch (e) {
            // Do something when catch error
        }

        uni.request({
            url: apiRoot + options.url,
            data: options.data,
            method: options.method ? options.method : 'POST',
            header: {
				'Cache-Control': 'no-cache',
				'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
				// 'Cookie': token?'JSESSIONID='+token:'',
				'XX-Device-Type': 'app',
				'XX-Api-Version': this.API_VERSION,
            },
            success: res => {
                var data = res.data;
                if (data.code == 10001) {
					 uni.showToast({
						title: '请重新登录',
						icon: 'none',
						duration: 1000
					});
					setTimeout(() => {
						this.login();
					}, 1000);

                } else {
                    options.success(data);
                }

            },
            fail: function (res) {
                if (options.fail) {
                    options.fail(res)
                }else{
					console.log(JSON.stringify(res))
				}
            },
            complete: options.complete ? options.complete : null
        });
    },
	login: function () {
		//uni.clearStorageSync()
		var userInfo=uni.getStorageSync('user');
		//console.log(userInfo);
		if(!userInfo || !userInfo.id){
			uni.reLaunch({
				url: '../login/login'
			});
			return false
		}
		if (!this.getToken()) {
			uni.reLaunch({
				url: '../login/login'
			});
			return false
		}
		return true
    },
    uploadFile(options) {

        options.url = this.API_ROOT + options.url;

        let token = this.getToken();

        let that = this;

        let oldSuccess  = options.success;
        options.success = function (res) {
            let data = JSON.parse(res.data);
            oldSuccess(data);
        }

        options.header = {
            'Content-Type': 'multipart/form-data'
        };
        return uni.uploadFile(options);

    },
    getToken() {
        var token = '';
        try {
            token = uni.getStorageSync('token')
        } catch (e) {
            // Do something when catch error
        }

        return token;
    },
    json2Form(json) {
        var str = []
        for (var p in json) {
            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(json[p]))
        }
        return str.join("&")
    },
//     timeFormat(second, fmt) {
//         let mDate = new Date();
//         mDate.setTime(second * 1000);
//         return mDate.Format(fmt);
//     },
    getUploadUrl(file) {
        if (file && file.indexOf('http') === 0) {
            return file;
        }
        return this.HOST + '/upload/' + file;
    }

};
